import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'dart:core';

import 'package:app/generated/app_strings.dart';
import 'package:app/src/config/app_data_cache.dart';
import 'package:app/src/http/bean/ywx_limit.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';

var _width = 120.0;
var _height = 30.0;

///添加到常用功能
void showPopuMenuWithUsed(BuildContext context, GlobalKey widgetKey,
    YwxItem item, GestureTapCallback? onTap) {
  String value = AppStrings.of(context).AddUsed;
  TextStyle style = AppStyle.textStyleWithItem();
  TextPainter? painter = TextUtils.calculateTextPainter(context, value, style);
  if (painter != null) {
    _width = painter.width + 24;
    _height = painter.height + 12;
  } else {
    _width = 100 + 24;
    _height = 20 + 12;
  }
  Widget widget = Container(
    width: _width,
    height: _height,
    child: _bubbleWidget(context, value, style, () {
      //点击添加到常用功能
      AppDataCache.addUsedItems(item.YwxId);
      onTap?.call(); //用于界面更新
      AppRouteHelper.pop(context); //关闭
    }),
  );
  var position = getWidgetGlobalRelativeRect(widgetKey,
      offset: Offset(-_width * 0.865, -_height));
  showWindow(
      context: context,
      position: position,
      windowBuilder: (BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return Material(
          child: widget,
          color: Colors.transparent,
        );
      });
}

Widget _bubbleWidget(BuildContext context, String value, TextStyle style,
    GestureTapCallback? onTap) {
  return BubbleWidget(
    _width, //fixme 气泡宽度(必填参数);宽度和高度无法自适应。
    _height, //fixme 气泡高度(必填参数)
    Colors.white, //fixme 背景色(必填参数)
    BubbleArrowDirection.bottom, //fixme 尖角位置(必填参数)
    length: 5,
    //length 边距(控制尖角的偏移位置。如果大于宽度或高度，或小于0；则尖角居中。)
    radius: 5.0,
    //radius 圆角半径
    arrHeight: 8.0,
    //arrHeight 尖角高度
    arrAngle: 45,
    //arrAngle 尖角角度（尖角两个边，展开的角度）
    borderColor: Colors.grey,
    //borderColor 边框颜色
    strokeWidth: 1.0,
    //strokeWidth 边框宽度；style:PaintingStyle.stroke才有效。
    style: PaintingStyle.stroke,
    //填充样式 PaintingStyle.fill
    innerPadding: 0.0,
    //innerPadding child子组件与气泡间距
    //child 子控件
    child: Center(
      child: InkWell(
          child: Text(
            value,
            style: style,
          ),
          onTap: onTap),
    ),
  );
}
